﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Reflection;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace RenameTracker
{    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();            

            // wire my observables to their underlying collections            
            _itemChooser.ItemsSource = _mruPathTracker;            
            _timeLine.DataContext = _historyTracker;
#if (DEBUG)
            _debugView.ItemsSource = _historyTracker;
            _debugView.IsEnabled = true;
#endif

            // populate interesting paths            
            UpdateMRU(_mruPathTracker, "$/Test-ConchangoV2/rentest/grand2/child2");
            UpdateMRU(_mruPathTracker, "$/CoatueScrum");
            UpdateMRU(_mruPathTracker, "$/Test-ConchangoV2/rentest/grand2/child2/parent/foo3.cs");
        }

        int _maxDropdownSize = 8;
        private static AppSettings _settings = AppSettings.Default;
        private ObservableCollection<string> _mruPathTracker = new ObservableCollection<string>();
        private ResetCollection<ChangeInfoExplained> _historyTracker = new ResetCollection<ChangeInfoExplained>();
        private ItemHistory _history = new ItemHistory();
        private TFS _tfs = null;
        private string _server = "njtfs";
        private bool _showDeletes { get { return _showDeletesBox.IsChecked ?? false; } }

        private void Go_Click(object sender, RoutedEventArgs args)
        {
            try
            {
                if (_tfs == null)
                {
                    _tfs = new TFS(_server);
                }

                GetHistory(_itemChooser.Text);
                UpdateMRU(_mruPathTracker, _itemChooser.Text);  
            }
            catch (Microsoft.TeamFoundation.TeamFoundationServerException e)
            {
                MessageBox.Show(e.Message, _settings.Title, MessageBoxButton.OK, MessageBoxImage.Asterisk);                
            }
        }

        private void GetHistory(string path)
        {
            var itemsAtPath = _tfs.GetSlotHistoryUnique(path);              
            
            if (itemsAtPath.Count > 1)
            {
                _history.Reset(ChooseItem(itemsAtPath), _showDeletes);
            }
            else
            {
                _history.Reset(itemsAtPath.Values.First(), _showDeletes);
            }
            _historyTracker.ResetTo(_history.History);
        }

        private Item ChooseItem(IDictionary<int, Item> itemsAtPath)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Update the MRU with a new item and return the modified list
        /// </summary>
        /// <param name="newItem"></param>
        /// <returns></returns>
        private void UpdateMRU<T>(ObservableCollection<T> mruList, T newItem)
        {
            mruList.Remove(newItem);
            mruList.Insert(0, newItem);
            if (mruList.Count > _maxDropdownSize)
            {
                mruList.RemoveAt(_maxDropdownSize - 1);
            }
        }
    }
}
